﻿Imports System.Data.SqlClient

Public Class frmDashboard
    Dim RnpPaciente As String
    Private Async Sub TextEdit1_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCodigoMembresia.KeyDown
        If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Enter Then
            Try
                If varConexion.State = ConnectionState.Open Then
                    varConexion.Close()
                End If

                Await varConexion.OpenAsync()

                Dim sqlCargarDatos As String = String.Format("SELECT a.Rnp_Paciente, a.Nombres, a.Apellidos, a.Fecha_Nacimiento, b.Sexo, a.Telefono, c.Tipo_Paciente FROM Paciente a INNER JOIN Sexo b ON a.Id_Sexo = b.Id_Sexo INNER JOIN Tipo_Paciente c ON a.Id_Tipo_Paciente = c.Id_Tipo_Paciente INNER JOIN Membresia d ON a.Rnp_Paciente = d.Rnp_Paciente WHERE d.Codigo_Membresia = '{0}'", Me.txtCodigoMembresia.EditValue)

                Dim cmd As New SqlCommand(sqlCargarDatos, varConexion)
                Dim reader As SqlDataReader = Await cmd.ExecuteReaderAsync()

                If Await reader.ReadAsync() Then
                    Me.txtRnpPaciente.EditValue = reader.Item("Rnp_Paciente")
                    RnpPaciente = Me.txtRnpPaciente.EditValue
                    Me.txtNombres.EditValue = reader.Item("Nombres")
                    Me.txtApellidos.EditValue = reader.Item("Apellidos")
                    Me.txtEdad.EditValue = CInt(DateDiff(DateInterval.Month, CDate(reader.Item("Fecha_Nacimiento")), Date.Today.Date) / 12)
                    Me.txtSexo.EditValue = reader.Item("Sexo")
                    Me.txtTelefono.EditValue = reader.Item("Telefono")
                    Me.txtTipoPaciente.EditValue = reader.Item("Tipo_Paciente")
                    Me.gboAccionesPaciente.Enabled = True
                    Me.gboHistorial.Enabled = True
                Else
                    MessageBox.Show("No se ha encontrado este paciente.", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Me.gboAccionesPaciente.Enabled = False
                    Me.gboHistorial.Enabled = False
                End If

                varConexion.Close()

                ObtenerHistorialPaciente()

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub txtCodigoMembresia_Enter(sender As Object, e As EventArgs) Handles txtCodigoMembresia.Enter
        Me.txtCodigoMembresia.EditValue = Nothing
    End Sub

    Private Sub txtCodigoMembresia_EditValueChanged(sender As Object, e As EventArgs) Handles txtCodigoMembresia.EditValueChanged
    End Sub

    Private Sub ObtenerHistorialPaciente()
        Try
            If varConexion.State = ConnectionState.Open Then
                varConexion.Close()
            End If

            varConexion.Open()
            Dim sqlHistorial As String = String.Format("SELECT COUNT(*) AS TotalAnalisis, (SELECT TOP 1 Fecha_Recepcion FROM Analisis WHERE Rnp_Paciente = '{0}' ORDER BY Id_Analisis DESC) AS FechaUltAnalisis FROM Analisis WHERE Rnp_Paciente = '{1}'", Me.txtRnpPaciente.EditValue, Me.txtRnpPaciente.EditValue)
            Dim cmd As New SqlCommand(sqlHistorial, varConexion)
            Dim reader As SqlDataReader = cmd.ExecuteReader

            If reader.Read Then
                Me.lblTotalAnalisis.Text = reader.Item("TotalAnalisis").ToString
                If IsDBNull(reader.Item("FechaUltAnalisis")) Then
                    Me.lblFechaUltAnalisis.Text = "No hay registro."
                Else
                    Me.lblFechaUltAnalisis.Text = CDate(reader.Item("FechaUltAnalisis")).ToLongDateString
                End If
            End If

            varConexion.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnNuevoAnalisis_Click(sender As Object, e As EventArgs) Handles btnNuevoAnalisis.Click
        Dim nuevoAnalisis As New frmNuevoAnalisi
        nuevoAnalisis.RnpPaciente = RnpPaciente
        nuevoAnalisis.MdiParent = Me.MdiParent
        nuevoAnalisis.Show()
    End Sub

    Private Sub txtCodigoMembresia_Click(sender As Object, e As EventArgs) Handles txtCodigoMembresia.Click
        Me.txtCodigoMembresia.EditValue = Nothing
    End Sub

    Private Sub btnActualizarInformacion_Click(sender As Object, e As EventArgs) Handles btnActualizarInformacion.Click
        Dim actualizarPaciente As New frmActualizarPaciente
        actualizarPaciente.RnpPaciente = RnpPaciente
        actualizarPaciente.MdiParent = Me.MdiParent
        actualizarPaciente.Show()
    End Sub

    Private Sub btnAsignarTarjeta_Click(sender As Object, e As EventArgs) Handles btnAsignarTarjeta.Click
        Dim asignarTarjeta As New frmAsignarTarjeta
        asignarTarjeta.RnpPaciente = RnpPaciente
        asignarTarjeta.MdiParent = Me.MdiParent
        asignarTarjeta.Show()
    End Sub

    Private Sub btnHistorialAnalisis_Click(sender As Object, e As EventArgs) Handles btnHistorialAnalisis.Click
        Dim visualizarResultados2 As New frmResultadosHistoricosPaciente
        visualizarResultados2.RnpPaciente = RnpPaciente
        visualizarResultados2.MdiParent = Me.MdiParent
        visualizarResultados2.Show()
    End Sub
End Class